$.namespace('report.scoreCnt');
report.regionScoreCnt = function() {

	return {
		init : function() {
			EasyUIInit.initControl(this.config.toolbar);
			EasyUIInit.initControl(this.config.grid);
			report.regionScoreCnt.loadData();
		},
		loadData : function() {
			$.ajax({
				type : 'POST',
				data : JSON.stringify({
					paramMap : {
						date1 : $('#date1').datebox('getValue'),
						date2 : $('#date2').datebox('getValue')
					}
				}),
				url : ctxRest + '/gcb/report/GetRegionScoreCount',
				contentType : 'application/json;charset=UTF-8',
				success : function(data) {
					data[data.length]={gridName:'合计：',regionName:'',value1:0,value2:0};
					var t1=data[data.length-1];
					var t2=data[data.length-2];
					for (var i = data.length-3; i >=0; i--) {
						if(data[i].gridID==data[i].regionID){
							t2=data[i];
							continue;
						}
						t1.value1+=data[i].value1;
						t1.value2+=data[i].value2;
						t2.value1+=data[i].value1;
						t2.value2+=data[i].value2;
						
						
					}
					
					$('#grid').datagrid({
						data : data
						
					});
				}
			});
		},
		getColumns : function() {
			var cols = [ [ {
				title : '广场名称',
				field : 'gridName',
				width : 200
			}, {
				title : '广场区域',
				field : 'regionName',
				width : 300
			}, {
				title : '保洁',
				field : 'value1',
				width : 150
			}, {
				title : '管理',
				field : 'value2',
				width : 150
			},{
				title : '合计',
				field : 't',
				width : 150,
				formatter : function(value, row, index) {
					var rc =  
					row.value1 + row.value2;
					return rc;

				}
			} ] ];

			return cols;
		}

	}
}();

report.regionScoreCnt.config = {
	grid : {
		id : "grid",
		type : "easyui-datagrid",
		option : {
			title : '',
			// url : ctxRest + '/gcb/report/GetRegionScoreCount',
			border : false,
			idField : 'id',
			fitColumns : false,
			pagination : false,
			columns : report.regionScoreCnt.getColumns(),
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#grid').datagrid('autoMergeCells', [ 'gridName' ]);
			}
		}
	},
	toolbar : {
		id : 'toolbar',
		type : 'div',
		isExternal : true,
		controls : [
				{
					id : 't1',
					type : 'span',
					isExternal : true,
					InnerHTML : '起始日期：'
				},
				{
					id : 'date1',
					type : 'easyui-datebox',
					option : {
						width : 100,
						value : function() {
							var date = new Date();
							var ctime = date.getFullYear() + '-'
									+ (date.getMonth() + 1) + '-'
									+ date.getDate();
							return ctime;
						}()
					}
				},
				{
					id : 't2',
					type : 'span',
					isExternal : true,
					InnerHTML : '截止日期：'
				},
				{
					id : 'date2',
					type : 'easyui-datebox',
					option : {
						width : 100,
						value : function() {
							var date = new Date();
							var ctime = date.getFullYear() + '-'
									+ (date.getMonth() + 1) + '-'
									+ date.getDate();
							return ctime;
						}()
					}
				}, {
					type : 'div',
					id : 'split',
					isExternal : true,
					attr : 'class="datagrid-btn-separator"'
				}, {
					type : 'easyui-linkbutton',
					id : 'btnSearch',
					option : {
						text : '查询',
						iconCls : 'icon-search',
						plain : true,
						onClick : report.regionScoreCnt.loadData
					}
				} ]
	}
};

$.extend($.fn.datagrid.methods, {
	autoMergeCells : function(jq, fields) {
		return jq.each(function() {
			var target = $(this);
			if (!fields) {
				fields = target.datagrid("getColumnFields");
			}
			var rows = target.datagrid("getRows");
			var i = 0, j = 0, temp = {};
			for (i; i < rows.length; i++) {
				var row = rows[i];
				j = 0;
				for (j; j < fields.length; j++) {
					var field = fields[j];
					var tf = temp[field];
					if (!tf) {
						tf = temp[field] = {};
						tf[row[field]] = [ i ];
					} else {
						var tfv = tf[row[field]];
						if (tfv) {
							tfv.push(i);
						} else {
							tfv = tf[row[field]] = [ i ];
						}
					}
				}
			}

			$.each(temp, function(field, colunm) {
				$.each(colunm, function() {
					var group = this;
					if (group.length > 1) {
						var before, after, megerIndex = group[0];
						for (var i = 0; i < group.length; i++) {
							before = group[i];
							after = group[i + 1];
							if (after && (after - before) == 1) {
								continue;
							}
							var rowspan = before - megerIndex + 1;
							if (rowspan > 1) {
								target.datagrid('mergeCells', {
									index : megerIndex,
									field : field,
									rowspan : rowspan
								});
							}
							if (after && (after - before) != 1) {
								megerIndex = after;
							}
						}
					}
				});
			});
		});
	}
});

$(function() {
	report.regionScoreCnt.init();
})